package com.iris.egrant.app.service.proposal;


import com.iris.egrant.app.model.proposal.ProposalCachedForm;
import com.iris.egrant.app.dao.proposal.ProposalAppDao;
import com.iris.egrant.app.model.proposal.LocalProposalCached;
import com.iris.egrant.dao.proposal.ProposalCachedExtendDao;
import com.iris.egrant.dao.proposal.ProposalCachedLocalDao;
import com.iris.egrant.dao.proposal.ProposalDao;
import com.iris.egrant.exception.ServiceException;
import com.iris.egrant.model.formInit.FormBaseLibrary;
import com.iris.egrant.model.proposal.Proposal;
import com.iris.egrant.model.proposal.ProposalCached;
import com.iris.egrant.model.proposal.ProposalCachedExtend;
import com.iris.egrant.remote.http.service.ServiceFactory;
import com.iris.egrant.service.formInit.InitializeService;
import com.iris.egrant.utils.XMLHelper;
import com.iris.egrant.web.struts2.Struts2Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("proposalLocalService")
@Transactional(rollbackFor = {Exception.class})
public class ProposalLocalServiceImpl implements ProposalLocalService {
    /*   85 */   private static final Logger LOGGER = LoggerFactory.getLogger(ProposalLocalServiceImpl.class);

    @Autowired
    private ProposalCachedLocalDao proposalCachedDao;

    @Autowired
    private InitializeService initializeService;


    @Autowired
    protected ProposalCachedExtendDao proposalCachedExtendDao;


    @Resource(name = "serviceFactory")
    private ServiceFactory serviceFactory;

    @Value("${domainegrant:}")
    private String domainPath;

    @Value("${uploadFileRootDir:}")
    private String uploadFileRootDir;
    /*      */
    @Autowired
    /*      */ private ProposalDao prpDao;

    /*      */   @Autowired
    /*      */   private ProposalAppDao prpAppDao;


    private Map<String, String> zhPrpStatus = new HashMap<String, String>();
    private Map<String, String> twPrpStatus = new HashMap<String, String>();
    private Map<String, String> enPrpStatus = new HashMap<String, String>();

    @Override
    public String saveProduct(Long paramLong1, Long paramLong2, String paramString) {
        return null;
    }

    @Override
    public void genPdf(Long paramLong) {

    }

    /*      */
    public void splitKnowledgePaper(Long posCode) {
        /* 1181 */
        this.prpDao.getSession().createSQLQuery("call init_knowledge_paper(?)").setLong(0, posCode.longValue()).executeUpdate();
        /*      */
    }

    @Override
    public String[] getproductxml(String paramString) {
        return new String[0];
    }


    public Map<String, Object> viewPrp(Map<String, Object> params, Long posCode) throws ServiceException, UnknownHostException {
        params.put("formType", "proposal");
//        params.put("psn_code", SecurityUtils.getCurrentUserId());
//        params.put("org_code", SecurityUtils.getCurrentOrgCode());
        params.put("actionType", Integer.valueOf(3));
        LocalProposalCached pc = this.proposalCachedDao.viewProposal(posCode);
        if (pc == null) {
            return null;
        }

        ProposalCachedExtend prpCachedExtend = this.proposalCachedExtendDao.getPrpInfo(posCode);

        FormBaseLibrary fbl = this.initializeService.getFormBaseLibraryByGrantId(null, pc.getFormCode(), XMLHelper.parseDoc(XMLHelper.StringToDoc(prpCachedExtend.getPrpXml())).asXML(), params);


        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("xmlData", fbl.getXmlData());
        resultMap.put("fbl", fbl);
        resultMap.put("pdfStatus", pc.getPdfStatus());

        return resultMap;
    }


    /*      */   public List<ProposalCachedForm> queryPrpFormList(String strGrantCode, String status) throws UnknownHostException {
        /*  176 */     List<ProposalCached> prpList = null;
        /*      */
        /*      */
        /*      */
        /*  180 */     List<ProposalCachedForm> formList = new ArrayList<ProposalCachedForm>();
        /*  181 */     prpList = this.proposalCachedDao.getProposalCachedListByPsnCode(100525299L,"20541", status);
        /*      */
        /*      */
        /*  184 */     String locale = Struts2Utils.getRequest().getParameter("locale");
        /*  185 */     Map<String, String> prpStatusMap = null;
        /*  186 */     if ("en_US".equals(locale)) {
            /*  187 */       prpStatusMap = this.enPrpStatus;
            /*  188 */     } else if ("zh_TW".equals(locale)) {
            /*  189 */       prpStatusMap = this.twPrpStatus;
            /*      */     } else {
            /*  191 */       prpStatusMap = this.zhPrpStatus;
            /*      */     }
        /*      */
        /*  194 */     ProposalCached prpCached = null;
        /*  195 */     for (int i = 0, size = prpList.size(); i < size; i++) {
            /*  196 */       prpCached = prpList.get(i);
            /*  197 */       prpCached.setPsnName("name");
            /*      */
            /*  199 */       Long grant_code = prpCached.getSubGrantCode();
            /*  200 */       String statYear = prpCached.getStatYear();
            /*  201 */       int count =1;
            /*      */
            /*  203 */       if ("02".equals(prpCached.getAuditStatus()) && count > 0) {
                /*  204 */         prpCached.setZh_cn_statusName("初审不通过");
                /*      */       } else {
                /*      */
                /*      */
                /*      */         try {
                    /*      */
                    /*  210 */           prpCached.setZh_cn_statusName(prpStatusMap.get(prpCached.getStatus()));
                    /*  211 */         } catch (Exception e) {
                    /*  212 */           LOGGER.error("获取申报书状态出错", e);
                    /*  213 */           prpCached.setZh_cn_statusName("");
                    /*      */         }
                /*      */       }
            /*      */
            /*      */
            /*  218 */       ProposalCachedForm form = new ProposalCachedForm();
//            /*  219 */       form.setRecommendOrgCode(prpCached.getRecommendOrg());
            /*  220 */       form.setContentFileCode(prpCached.getContentFileCode());
            /*  221 */       form.setContentPdfFileCode(prpCached.getContentPdfFileCode());
            /*  222 */       form.setEn_us_statusName(prpCached.getEn_us_statusName());
            /*  223 */       form.setEnTitle(prpCached.getEnTitle());
            /*  224 */       form.setFbl(prpCached.getFbl());
            /*  225 */       form.setFlagPdfType(prpCached.getFlagPdfType());
            /*  226 */       form.setFormCode(prpCached.getFormCode());
            /*  227 */       form.setGrantCode(prpCached.getGrantCode());
            /*  228 */       form.setGrantName(prpCached.getGrantName());
            /*  229 */       form.setHelpGrantCode(prpCached.getHelpGrantCode());
            /*  230 */       form.setHelpGrantName(prpCached.getHelpGrantName());
            /*  231 */       form.setOldStatus(prpCached.getOldStatus());
            /*  232 */       form.setOrgCode(prpCached.getOrgCode());
            /*  233 */       form.setOrgName(prpCached.getOrgName());
            /*  234 */       form.setPdfFileCode(prpCached.getPdfFileCode());
            /*  235 */       form.setPdfGenTime(prpCached.getPdfGenTime());
            /*  236 */       form.setPdfReqTime(prpCached.getPdfReqTime());
            /*  237 */       form.setPdfStatus(prpCached.getPdfStatus());
            /*  238 */       form.setPdfVersion(prpCached.getPdfVersion());
            /*  239 */       form.setPosCode(prpCached.getPosCode());
            /*  240 */       form.setPrjPsnCode(prpCached.getPrjPsnCode());
            /*  241 */       form.setPrpVersion(prpCached.getPrpVersion());
            /*  242 */       form.setPsnCode(prpCached.getPsnCode());
            /*  243 */       form.setPsnName(prpCached.getPsnName());
            /*  244 */       form.setStatus(prpCached.getStatus());
            /*  245 */       form.setStatYear(prpCached.getStatYear());
            /*  246 */       form.setSubGrantCode(prpCached.getSubGrantCode());
            /*  247 */       form.setSubGrantName(prpCached.getSubGrantName());
            /*  248 */       form.setSubmitDate(prpCached.getSubmitDate());
            /*  249 */       form.setUpdateDate(prpCached.getUpdateDate());
            /*  250 */       form.setZh_cn_statusName(prpCached.getZh_cn_statusName());
            /*  251 */       form.setZhTitle(prpCached.getZhTitle());
            /*  252 */       formList.add(form);
            /*      */     }
        /*  254 */     return formList;
        /*      */   }

    @Override
    public FormBaseLibrary viewPrpByPrpCode(Map<String, Object> paramMap, Long paramLong) throws ServiceException {
        return null;
    }

    @Override
    public Proposal getProposalByCode(Long paramLong) {
        return null;
    }

    @Override
    public Map<String, Object> getPrpLoad(Map<String, Object> paramMap, Long paramLong) throws ServiceException, UnknownHostException {
        return null;
    }

    @Override
    public void setReturnMap(Map<String, Object> paramMap, String paramString1, String paramString2, String paramString3, Long paramLong) {

    }

    /*      */
    public void initPrjProgressData(Long keyCode, String progressCode, String type) {
        /* 1133 */
        this.prpAppDao.getSession().createSQLQuery("call proc_insert_prj_progress('" + keyCode + "','" + progressCode + "','" + "'test'" + "','" + type + "')").executeUpdate();
        /*      */
    }


}